﻿using Encloser.Json;
using Encloser.Packed;
using System;
using System.IO;
using System.Text;

namespace Encloser.Toolkit
{
    public class FileIO
    {
        public static Result<T> Read<T>(string path)
        {
            if (string.IsNullOrEmpty(path)) return Result<T>.INVALID_PARAMETERS;
            try
            {
                if (!File.Exists(path))
                {
                    var path_ = path + "_";
                    if (File.Exists(path_)) File.Move(path_, path);
                    else return Result<T>.Failed("读取路径不存在");
                }
                using (var fs = new FileStream(path, FileMode.Open)) return JsonParser.Parse<T>(fs);
            }
            catch (Exception e)
            {
                return Result<T>.Failed("读取数据失败", e);
            }
        }

        public static Result<string> Read(Stream stream)
        {
            if (stream == null) return Result<string>.INVALID_PARAMETERS;
            if (!stream.CanRead) return Result<string>.Failed("流不可读");
            try
            {
                stream.Position = 0;
                var data = new StreamReader(stream).ReadToEnd();
                return Result<string>.Success(data ?? string.Empty);
            }
            catch (Exception e)
            {
                return Result<string>.Failed(e);
            }
        }

        public static Result<string> Read(string path)
        {
            if (string.IsNullOrEmpty(path)) return Result<string>.INVALID_PARAMETERS;
            try
            {
                var data = File.ReadAllText(path);
                return Result<string>.Success(data ?? string.Empty);
            }
            catch (Exception e)
            {
                return Result<string>.Failed(e);
            }
        }

        public static Result Write(string path, object data)
        {
            if (ValidUtils.IsEmpty(path, data)) return Result.INVALID_PARAMETERS;
            var path_ = path + "_";
            return JsonParser.ToJsonFile(data, path_).Try(() =>
            {
                File.Delete(path);
                File.Move(path_, path);
                return true;
            });
        }

        public static Result Write(string path, string text)
        {
            if (string.IsNullOrEmpty(path)) return Result.INVALID_PARAMETERS;
            try
            {
                File.WriteAllText(path, text ?? string.Empty);
                return Result<bool>.Success(true);
            }
            catch (Exception e)
            {
                return Result<bool>.Failed(e);
            }
        }

        public static Result Write(Stream stream, string text)
        {
            if (stream == null) return Result.INVALID_PARAMETERS;
            if (!stream.CanWrite) return Result.Failed("流不可写");
            try
            {
                var data = Encoding.UTF8.GetBytes(text ?? string.Empty);
                stream.SetLength(data.Length);
                stream.Position = 0;
                stream.Write(data, 0, data.Length);
                stream.Flush();
                return Result.OK;
            }
            catch (Exception e)
            {
                return Result.Failed(e);
            }
        }
    }
}
